package com.china08.yxyapi.api.manage.sch.platform.xiaoben.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonAuthRepository;
import com.china08.yxyapi.service.db.YxyAuthService;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class KeChengAuthStuListController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonAuthRepository authRepository;

    @Autowired
    YxyAuthService yxyAuthServiceImpl;

    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/auth/kecheng/stu/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        String classId = reqModel.classId;
        String keyword = StringUtils.isBlank(reqModel.keyword) ? "" : "%" + reqModel.keyword + "%";
        Integer pageNum = reqModel.pageNum;

        Integer allSize = 0;
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> map = yxyAuthServiceImpl.authUserList(lessonId, classId, keyword, pageNum);

        if (null != map) {
            allSize = Integer.parseInt(map.get("allSize") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }

        List<Pojo> returnList = new ArrayList<Pojo>();
        for (Map<String, Object> m : list) {
            Pojo pojo = new Pojo();
            pojo.userId = m.get("user_id") + "";
            pojo.username = m.get("username") + "";
            pojo.name = m.get("user_nick") + "";
            pojo.classNick = m.get("class_name") + "";
            pojo.createdDate = m.get("created_date") + "";
            pojo.guardianNick = m.get("guardian_name") + "";
            Object obj1 = m.get("last_modified_date");
            if (obj1 != null) {
                pojo.lastModifiedDate = DateToolUtils.format((Date) m.get("last_modified_date"), "yyyy-MM-dd HH:mm:ss");
            }
            returnList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.allSize = allSize;
        respModel.list = returnList;
        return respModel;

    }

    public static class Pojo {
        public String userId;
        public String username;
        public String name;
        public String classNick;
        public String createdDate;
        public String guardianNick;
        public String lastModifiedDate;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String lessonId;
        public String keyword;
        public String classId;
        public Integer pageNum;
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allSize;
        public List<Pojo> list;
    }
}
